#include "rs422_serial_port.h"
#include "ui_rs422_serial_port.h"

rs422_serial_port::rs422_serial_port(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::rs422_serial_port)
{
    ui->setupUi(this);
    connect(ui->rs422ReturnMenu, &QPushButton::clicked, this, &rs422_serial_port::pushReturnMenu);

    /***********************/
    const char* portName = "/dev/pts/3";
    int serialFd = open(portName, O_RDWR | O_NOCTTY);
    if (serialFd < 0)
    {
        std::cout << "连接串口失败" << std::endl;
    }
    else
    {
        std::cout << "连接串口成功" << std::endl;
    }

    struct termios tty;
    memset(&tty, 0, sizeof(tty)); // 配置串口参数
    cfsetospeed(&tty, B9600);
    cfsetispeed(&tty, B9600); // 波特率设置为 9600
    tty.c_cflag |= CS8; // 数据位设置为 8 位
    tty.c_cflag &= ~CRTSCTS; // 禁用硬件流控制
    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // 禁用软件流控制
    tty.c_cflag |= PARENB;
    tty.c_cflag |= PARODD; // 使能奇偶校验，设置为奇校验
    tty.c_cflag &= ~CSTOPB; // 设置停止位为 1 位
    tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // 禁用本地模式和回显
    tty.c_cc[VMIN] = 0;
    tty.c_cc[VTIME] = 10; // 设置读取超时时间为 1 秒

    if (tcsetattr(serialFd, TCSAFLUSH, &tty) != 0) {
        std::cout << "无法设置串口参数" << std::endl;
    }
    /***********************/


    layout = new QVBoxLayout();

     m_serialPort=new QSerialPort();
     m_serialPort->setPortName("/dev/pts/19");
     m_serialPort->setBaudRate(QSerialPort::Baud9600);
     m_serialPort->setDataBits(QSerialPort::Data8);
     m_serialPort->setStopBits(QSerialPort::OneStop);
     m_serialPort->setParity(QSerialPort::EvenParity);

     if(m_serialPort->open(QIODevice::ReadWrite))
     {
         qDebug()<<"连接串口成功";
     }
     else
     {
         qDebug()<<"连接串口失败";
     }

     connect(ui->getAllSerialPorts, &QPushButton::clicked, this, &rs422_serial_port::getAvailablePorts);
     connect(m_serialPort, &QSerialPort::readyRead, this, &rs422_serial_port::readSerialInfo);
     connect(ui->sendSerialInfo, &QPushButton::clicked, this, &rs422_serial_port::sendSerialInfo);

}

rs422_serial_port::~rs422_serial_port()
{
    delete ui;
}

void rs422_serial_port::pushReturnMenu()
{
    emit returnMenu();
}

void rs422_serial_port::getAvailablePorts()
{
    QList<QSerialPortInfo> serialPorts = QSerialPortInfo::availablePorts();
    QStringList m_serialPortName;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        m_serialPortName << info.portName();
        qDebug()<<"serialPortName:"<<info.portName();
    }
    setScrollArea(m_serialPortName);
}

void rs422_serial_port::setScrollArea(QStringList &SerialPorts)
{
    delete layout;

    QStringList yourStringList;
    yourStringList << " 111" << " 222" << " 333" << " 444" << " 555";
    layout = new QVBoxLayout();
    foreach(const QString& item, SerialPorts)
    //foreach(const QString& item, yourStringList)
    {
        QLabel* label = new QLabel(item);
        layout->addWidget(label);
    }
    ui->scrollAreaCotent->setLayout(layout);
    ui->scrollArea->setWidget(ui->scrollAreaCotent);
}

void rs422_serial_port::sendSerialInfo()
{
    while(!TCP_SendMsg_queue.isEmpty())
    {
        QString sendData_QS = TCP_SendMsg_queue.dequeue();
        QByteArray sendData_QBA = sendData_QS.toUtf8();
        m_serialPort->write(sendData_QBA);
    }
}

void rs422_serial_port::readSerialInfo()
{
    QByteArray buf;
    buf = m_serialPort->readAll();
    qDebug() << "这是一条我读到的消息：" << buf;
}
